﻿using Pool;
using UnityEngine;
using Resources = Bingo.Facades.Resources;

namespace Bingo.Game
{
    public class PoolManager : MonoBehaviour
    {
        DictionaryObjectPool _objectpool;

        public const string BLOCK_POOL = "BLOCK_POOL";

        public const string BULLET_POOL = "BULLET_POOL";

        public const string ANCHOR_POOL = "ANCHOR_POOL";

        public const string EXPLOSION_POOL = "EXPLOSION_POOL";

        public const string BOOM_POOL = "BOOM_POOL";

        public const string GOO_POOL = "GOO_POOL";

        public const string DOT_POOL = "DOT_POOL";
        
        public const string TRIANGLE1_POOL = "TRIANGLE1_POOL";
        
        public const string TRIANGLE2_POOL = "TRIANGLE2_POOL";
        
        public const string TRIANGLE3_POOL = "TRIANGLE3_POOL";
        
        public const string TRIANGLE4_POOL = "TRIANGLE4_POOL";

        public static PoolManager Instance;

        private void Awake()
        {
            Instance = this;
            this._objectpool = new DictionaryObjectPool();
        }

        void Start()
        {
        }

        public void Init()
        {
            InitPoolByName(BLOCK_POOL, "BlockPrefab", 400);
            InitPoolByName(BULLET_POOL, "BulletPrefab", 30);
            InitPoolByName(ANCHOR_POOL, "AnchorPrefab", 5);
            InitPoolByName(EXPLOSION_POOL, "EFF_Explosion", 20);
            InitPoolByName(BOOM_POOL, "EFF_X_Boom", 6);
            InitPoolByName(DOT_POOL, "DotPrefab", 50);
            InitPoolByName(TRIANGLE1_POOL, "Triangle1BlockPrefab", 10);
            InitPoolByName(TRIANGLE2_POOL, "Triangle2BlockPrefab", 10);
            InitPoolByName(TRIANGLE3_POOL, "Triangle3BlockPrefab", 10);
            InitPoolByName(TRIANGLE4_POOL, "Triangle4BlockPrefab", 10);
        }

        private void InitPoolByName(string poolName, string prefabName, int count)
        {
            GameObject prefab = Resources.LoadSync<GameObject>(BAssetBundle.Gameresources.BundleName, prefabName);
            InitPool(poolName, prefab, count);
        }


        private void InitPool(string poolName, GameObject prefab, int num = 100)
        {
            GameObject parent = new GameObject(poolName);
            parent.transform.SetParent(this.transform);
            this._objectpool.AddObjectPool(poolName, prefab, parent.transform, num);
        }


        public GameObject Spawn(string poolName)
        {
            return this._objectpool[poolName].Spawn();
        }

        public void DeSpawn(string poolName, GameObject go)
        {
            this._objectpool[poolName].Despawn(go);
        }

        public Pool.ObjectPool GetObjectPool(string poolName)
        {
            return this._objectpool[poolName];
        }
    }
}